package com.ml.mall.service.shop.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ml.mall.bean.page.Page;
import com.ml.mall.bean.req.shop.ShopSupplierQueryDto;
import com.ml.mall.bean.res.product.SysProductQueryByRandomWith18Res;
import com.ml.mall.bean.res.product.SysProductShopPageRes;
import com.ml.mall.bean.res.shop.ShopSupplierQueryRes;
import com.ml.mall.bean.vo.coupon.CouponVo;
import com.ml.mall.entity.deduct.SysADeduct;
import com.ml.mall.mapper.company.SysCompanyMapper;
import com.ml.mall.mapper.company.SysCompanyShippingMapper;
import com.ml.mall.mapper.product.SysProductMapper;
import com.ml.mall.service.coupon.SysCouponService;
import com.ml.mall.service.deduct.SysADeductService;
import com.ml.mall.service.shop.ShopService;
import com.ml.mall.utils.OrderPriceUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *   @description    :description 
 *   @author         :zxm
 *   @date           :2021/5/26
 *   @version        :1.0
**/
@Slf4j
@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    private SysProductMapper sysProductMapper;
    @Autowired
    private SysCouponService sysCouponService;
    @Autowired
    private SysADeductService sysADeductService;
    @Override
    public Page<ShopSupplierQueryRes> queryPageShopSupplier(ShopSupplierQueryDto dto) {
        // 处理条件;默认是按照id降序排列
        if(!StringUtils.hasText(dto.getOrderBy())){
            dto.setOrderBy("t1.id");
        }
        if("stars_num".equals(dto.getOrderBy())){
            dto.setOrderBy("t2.stars_num");
        }
        Page<ShopSupplierQueryRes> page = new Page<>(dto.getPageIndex(),dto.getPageNum());
        //买家名称：应该是代码
        List<ShopSupplierQueryRes> shopSupplierList = sysProductMapper.selectPageShopSupplier(dto);
        Long count = sysProductMapper.countShopSupplier(dto);
        for (ShopSupplierQueryRes shopSupplier:shopSupplierList) {
            // 查询卖家产品数量最多的5个目录名称
            List<String> supplierCategoryList = sysProductMapper.selectSupplierCategory(shopSupplier.getCompanyId());
            String productNameStr = String.join("/", supplierCategoryList);
            shopSupplier.setCategoryStrs(productNameStr);
        }
        page.setTotal(Long.valueOf(count).intValue());
        page.setRecords(shopSupplierList);
        return page;
    }

    @Override
    public List<SysProductShopPageRes> recognizeShopHaveCoupponPageRes(List<SysProductShopPageRes> pageDate) {
        //检测当前是否有全局优惠券；如果有直接全部都返回
        if(sysCouponService.checkHaveAdminCouppon()){
            pageDate.stream().forEach(item->item.setHaveCoupon(true));
            return pageDate;
        }
        //检索是否有有优惠券
        for(int i = 0; i<pageDate.size();i++){
            CouponVo couponVo = new CouponVo();
            couponVo.setVariantId(pageDate.get(i).getVariantId());
            couponVo.setSupplierId(pageDate.get(i).getCompanyId());
            if(sysCouponService.queryUsableCoupon(couponVo).size() > 0){
                SysProductShopPageRes sysProductShopPageRes = pageDate.get(i);
                sysProductShopPageRes.setHaveCoupon(true);
                pageDate.set(i,sysProductShopPageRes);
            }
        }
        return pageDate;
    }

    @Override
    public List<SysProductQueryByRandomWith18Res> recognizeShopHaveCoupponPageRandom18Res(List<SysProductQueryByRandomWith18Res> pageDate) {
        //检测当前是否有全局优惠券；如果有直接全部都返回
        if(sysCouponService.checkHaveAdminCouppon()){
            pageDate.stream().forEach(item->item.setHaveCoupon(true));
            return pageDate;
        }
        //检索是否有有优惠券
        for(int i = 0; i<pageDate.size();i++){
            CouponVo couponVo = new CouponVo();
            couponVo.setVariantId(pageDate.get(i).getVariantId());
            couponVo.setSupplierId(pageDate.get(i).getCompanyId());
            if(sysCouponService.queryUsableCoupon(couponVo).size() > 0){
                SysProductQueryByRandomWith18Res sysProductShopPageRes = pageDate.get(i);
                sysProductShopPageRes.setHaveCoupon(true);
                pageDate.set(i,sysProductShopPageRes);
            }
        }
        return pageDate;
    }

    public static void main(String[] args) {
        List<SysProductShopPageRes> listInt = new ArrayList<>();
        listInt.add(new SysProductShopPageRes());
        listInt.add(new SysProductShopPageRes());
        listInt.add(new SysProductShopPageRes());
        listInt.add(new SysProductShopPageRes());
        listInt.add(new SysProductShopPageRes());
        listInt.stream().forEach(item->item.setHaveCoupon(true));
        System.out.println(listInt);
    }

}
